using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Master : System.Web.UI.MasterPage
{
    #region "Public Properties"
    private string _RootDir;
    #endregion

    protected void Page_Init(object sender, EventArgs e)
    {
        _RootDir = Request.ApplicationPath;
        if(_RootDir != "/")
            _RootDir += "/";
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        LoginStatus1.LogoutPageUrl = "login.aspx?ReturnUrl=" + Request.RawUrl;
        AddCSS("CSS/BlueStripe.css");
        litMainMenu.Text = string.Format("{0}", DisplaySiteMapLevelAsBulletedList(smSiteMap));
    }

    #region "Client Javascript Builder"
    public void AddJS(string JSfile)
    {
        if(JSfile.Substring(0, 4) != "http")
            JSfile = string.Format("{0}{1}", _RootDir, JSfile);

        HtmlGenericControl hgcJS = new HtmlGenericControl("script");
        hgcJS.Attributes.Add("language", "javascript");
        hgcJS.Attributes.Add("type", "text/javascript");
        hgcJS.Attributes.Add("src", JSfile);
        Page.Header.Controls.AddAt(3, hgcJS);
    }
    #endregion

    #region "Client CSS Builder"
    public void AddCSS(string cssFile)
    {
        AddCSS(cssFile, "*");
    }
    public void AddCSS(string cssFile, string media)
    {
        if(cssFile.Substring(0, 4) != "http")
            cssFile = string.Format("{0}{1}", _RootDir, cssFile);

        HtmlGenericControl cssLink = new HtmlGenericControl("style");
        cssLink.Attributes.Add("type", "text/css");
        if(media != "*")
            cssLink.Attributes.Add("media", media);
        cssLink.InnerHtml = String.Concat("\n<!-- \n", "@import url(" + cssFile + ");", " \n-->\n");
        Page.Header.Controls.Add(cssLink);
    }
    #endregion

    #region "Navigation Builder"
    private string DisplaySiteMapLevelAsBulletedList(SiteMapDataSource smdsNavigation)
    {
        //Get the SiteMapDataSourceView from the siteMapData SiteMapDataSource
        SiteMapDataSourceView siteMapView = (SiteMapDataSourceView)smdsNavigation.GetView(String.Empty);
        //Get the SiteMapNodeCollection from the SiteMapDataSourceView
        SiteMapNodeCollection nodes = (SiteMapNodeCollection)siteMapView.Select(DataSourceSelectArguments.Empty);
        ///Recurse through the SiteMapNodeCollection
        return GetSiteMapLevelAsBulletedList(nodes);
    }
    
    private string GetSiteMapLevelAsBulletedList(SiteMapNodeCollection nodes)
    {
        string Output = String.Empty;
        string LinkClass = "navItemInactive";
        string CurrentPage = Request.ServerVariables["SCRIPT_NAME"];

        foreach (SiteMapNode node in nodes)
        {

            if (CurrentPage.ToLower() == node.Url.ToLower())
            {
                LinkClass = "navItemActive";
                Page.Title = node.Title;
            }

            Output += String.Format("<li><a class=\"{0}\" href=\"{1}\" >{2}</a></li>", LinkClass, node.Url, node.Title);
            LinkClass = "navItemInactive";
        }
        return Output;
    }
    #endregion
}